package com.just521.xinshoutong.common;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.KeyEvent;

import com.just521.xinshoutong.xml.CommonXmlParser;


public class CommonDefine{
	public static int 						g_int_Invalid_ID 	= -1;
	public static int 						g_int_Invalid_Time 	= -1;
	public static int 						E_FAIL	= -1;
	public static int 						S_OK	= 0;
	public static int 						Invalid_Value	= -1;
	
	
	public final static long DRAG_LIST_LENGTH 	= 10; 
	public final static long PARENTMSGID_ROOT 	= 0; 
	public final static long STARTNO_INITIAL 	= 0; 	
	public final static long REQUESTCOUNT_LIMIT = 20;
	public final static long BLOCK_DEFAULT = 0;	
	public static String gPackageVer="";
	public static String gSoftType="10";
	public static String gSoftChannel="0";
	public static Context gappctx;
	public static class ColorDefine{
		public static int			table_header_text  	= 0xffffffff;
		public static int			table_header_bkg  	= 0xFFFF6600;
		public static int			table_cell_text 	= 0xff303030;
		public static int			table_row_bkg  		= 0x00000000;
		public static int			table_boder  		= 0xFFFFB90F;
	}
   
	public static class SkinSource{
		public static int g_ValidTextColor = Color.BLUE;
		public static int g_InvalidTextColor = Color.GRAY;
		public static int g_HighLight_TextColor = Color.BLACK;
		public static int g_HighLight_TextColor_bg = Color.YELLOW;
		public static int g_login_TextColor = 0xff274e6c;
		public static int g_background_editNote_Remind = 0x00000000;
	}
    public static class  SpecialSymbol {

		public static String 	 	fenhao= "&fenhao";
		public static String 	 	douhao= "&douhao";
		public static String 	 	jinghao= "&jinghao";
		public static String 	 	maohao= "&maohao";
		public static String 	 	tanhao= "&tanhao";
			
    }
    
  
    public static class Screen{
    	public static  float density = 0;
        public static  int screen_w = 0;
        public static  int screen_h = 0;
    }
    
 
    public	class	ProcessEnum
	{
		public static final int Process_1 = 1;
		public static final int Process_3 = 3;
		public static final int Process_5 = 5;
		public static final int Process_7 = 7;		
		public static final int Process_9 = 9;	
	}     
   
    
	
	public	class	RowAttrTypeEnum
	{
		public static final long RowAttrType_SingleLine = 0x00000001;	
		public static final long RowAttrType_EclipseEnd = 0x00000002;
	}
    private static Resources gResources;
    public static Resources getResources(Context ctx){
    	if(gResources==null){
    		if(ctx!=null){
    			gResources=ctx.getResources();
    		}
    	}
    	return gResources;
    }
    public static String getStringContent(Context ctx, int id ){
		return CommonDefine.getResources(ctx).getString(id);
	}

	
	public class OnlineParam{
		public static final String		Param_official_toast ="official_toast";
		public static final String		Param_hosturl ="hosturl";
		public static final String		Param_isCheckUpdate ="isCheckUpdate";

		public static final String		Param_tuoqq ="tuoqq";
		public static final String		Param_vip_tuiguang_rate_man ="vip_tuiguang_rate_man";
		public static final String		Param_viplimit_normalmember_msg ="viplimit_normalmember_msg";
		
		public static final String		Param_sina_limit_flag ="sina_limit_flag";
		public static final String		Param_weibo_content ="weibo_content";
		public static final String		Param_weibo_shaitu_content ="weibo_shaitu_content";
		public static final String		Param_weibo_nickname ="weibo_nickname";
		
		public static final String		Param_sms_content ="sms_content";
		public static final String		Param_weibo_down_url ="weibo_down_url";
		public static final String		Param_weibo_pic_url ="weibo_pic_url";

		//加密key
		public static final String		Param_encrypt_key ="encrypt_key";
		public static final String		Param_kaixindoutormb ="kaixindoutormb";
		
		
		public static final String		Param_Profit_Wall_one ="wall_one";
		public static final String		Param_Profit_Wall_two ="wall_two";
		public static final String		Param_Profit_Wall_three ="wall_three";
		public static final String		Param_Profit_Wall_four ="wall_four";
		
		public static final String		Param_vip_oneyear ="vip_oneyear";
		
//		public static final String		Param_award_yijifen_2 ="award_yijifen_2";
//		
//		public static final String		Param_shop_jiage_changeword ="shop_jiage_changeword";
//		public static final String		Param_shop_jiage_tips ="shop_jiage_tips";
//		public static final String		Param_shop_jiage_sex ="shop_jiage_sex";
//		public static final String		Param_shop_jiage_sex_month ="shop_jiage_sex_month";
//		public static final String		Param_shop_jiage_msg ="shop_jiage_msg";
//		public static final String		Param_shop_jiage_msg_month ="shop_jiage_msg_month";
//		public static final String		Param_shop_jiage_place ="shop_jiage_place";
//		public static final String		Param_shop_jiage_color ="color_price";
//		public static final String		Param_shop_jiage_gift ="shop_jiage_gift";
//		
//		//tg
//		
//		public static final String		Param_goapk_soft_id="goapk_soft_id";
//		public static final String		Param_goapk_switch_shua="goapk_switch_shua";
//		
//		public static final String		Param_hiapk_soft_id="hiapk_soft_id";
//		public static final String		Param_hiapk_switch_shua="hiapk_switch_shua";
//		public static final String		Param_hiapk_soft_probability	="hiapk_soft_probability";
//		
//		public static final String		Param_pointjoy="pointjoy";
		
		
		
		
	}
	public class UmengStatiscs{
		public static final String		Event_buyticket ="buyticket";
		public static final String		Event_buyticket1 ="buyticket1";
		public static final String		Event_buyticket2 ="buyticket2";
		public static final String		Event_buyticket3 ="buyticket3";
		public static final String		Event_buyticket4 ="buyticket4";
		public static final String		Event_buyticket6 ="buyticket6";
		
		public static final String		Event_bingo ="bingo";
		
		public static final String		Event_paychance ="paychance";
		
		public static final String		Event_signon ="signon";
		public static final String		Event_invited ="invited";
		
	}
	
	static class ButtonHandler extends Handler
	{

		private WeakReference < DialogInterface > mDialog;
		
		public ButtonHandler(DialogInterface dialog)
		{
		mDialog =   new WeakReference < DialogInterface > (dialog);
		}
		
		public   void handleMessage(Message msg)
		{
			switch (msg.what)
			{
			
				case DialogInterface.BUTTON_POSITIVE:
				case DialogInterface.BUTTON_NEGATIVE:
				case DialogInterface.BUTTON_NEUTRAL:
				((DialogInterface.OnClickListener) msg.obj).onClick(mDialog
				.get(), msg.what);
				break ;
			}
		}
	}
	public static class DialogRuntimeProcessor{
		
		static public void replaceDialogButtonHandler(Dialog dlg){
			try
			{
				java.lang.reflect.Field field = dlg.getClass().getDeclaredField("mAlert");
				field.setAccessible( true );
				Object obj = field.get(dlg);
				field = obj.getClass().getDeclaredField( "mHandler" );
				field.setAccessible( true );
				field.set(obj, new ButtonHandler(dlg));
			}
			catch (Exception e)
			{
			}
		}
		
	}
	
	public static Date format2Date(String dateStr){
		Date d = new Date();
		
		if(dateStr!=null && !dateStr.equals("")){
			SimpleDateFormat   f   =   new   SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
			try{
				d   =   f.parse( dateStr );
			}catch(ParseException e){
			}
		}
		return d;
	}

	public static boolean appIsInstalled(Context context,String pageName){
    	try {
			PackageInfo packinfo = context.getPackageManager().getPackageInfo(pageName,0);
			return true;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			return false;
		}
    }
	public static String getSourceUser(Context context){
		String ret=null;
		String tuiguangren =""/*(String)UserInfo.get("tuiguangren")*/;
		if(tuiguangren!= null && tuiguangren.length()>0){
			ret = tuiguangren;
		}else{
			
			Resources r=getResources(context);
			AssetManager am=r.getAssets();
			
			String destfile=null;
			try{
				String[] list=am.list("");
				for(String item:list){
					if(item.contains("sourceuser")){
						destfile=item;
					}
				}
				if(destfile!=null){
					
					InputStream im=am.open(destfile);
					String buf=inputStream2String(im);
					CommonXmlParser xmlp=new CommonXmlParser();
					xmlp.parse(buf);
					HashMap<String,Object> xmlret=xmlp.getParseResult();
					ret=(String)xmlret.get("sourceuserid");
				}
			}catch(IOException e){
				
			}catch(XmlPullParserException e){
				
			}
		}
		return ret;
	}
	
	public static String getwifi(Context ctx)
	{
		String code="";
		 WifiManager wifi = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);  
	     WifiInfo info = wifi.getConnectionInfo();
	     if(info==null){
	     }
	     else{		     
	    	 code= info.getMacAddress(); 
		     if(code==null || code.length()==0){
		    	 code="";
		     }
	     }
	     
	     return code;
	}
	
	public static String getmcode(Context ctx)
	{
    	 TelephonyManager tm = (TelephonyManager)ctx.getSystemService(Context.TELEPHONY_SERVICE);  
    	 String mcode = tm.getDeviceId();  
    	 if(mcode==null || mcode.length()==0){
    		 mcode="";
    	 }
    	 return mcode;
	}
	
	public static String getresolution(Activity ctx)
	{
		DisplayMetrics dm = new DisplayMetrics();
		ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return String.format("%d*%d", dm.widthPixels,dm.heightPixels);
	}
	
	
	
	public   static   String   inputStream2String(InputStream   is)   throws   IOException{ 
		ByteArrayOutputStream   baos   =   new   ByteArrayOutputStream(); 
		int   i=-1; 
		while((i=is.read())!=-1){ 
		baos.write(i); 
		} 
		return   baos.toString(); 
	}
	public static class AlixOnCancelListener implements
			DialogInterface.OnCancelListener {
		Activity mcontext;
		
		public AlixOnCancelListener(Activity context) {
			mcontext = context;
		}
		
		public void onCancel(DialogInterface dialog) {
			mcontext.onKeyDown(KeyEvent.KEYCODE_BACK, null);
		}
	}
}


